using System.Diagnostics;
using System.Text;
using System.Web;

namespace CKS.EBE.Linkbacks
{
	public class LocalPostInfo
	{
		public string WebUrl { get; set; }
		public int ItemId { get; set; }

		public string Title { get; set; }

		/// <summary>
		/// Absolute URL for the post
		/// </summary>
		public string Url { get; set; }

		private string _Excerpt = string.Empty;

		public string Excerpt
		{
			[DebuggerStepThrough]
			get { return _Excerpt; }
			set { _Excerpt = value ?? string.Empty; } // prevent null assignment
		}

		private string _BlogName = string.Empty;

		public string BlogName
		{
			[DebuggerStepThrough]
			get { return _BlogName; }
			set { _BlogName = value ?? string.Empty; }
		}

		public LocalPostInfo()
		{
			Url = string.Empty;
		}

		public byte[] GetBytesForTrackbackPingHttpPost()
		{
			var postVariables = new StringBuilder(100);
			postVariables.Append("url=").Append(HttpUtility.UrlEncode(Url));
			postVariables.Append("&title=").Append(HttpUtility.UrlEncode(Title));
			postVariables.Append("&excerpt=").Append(HttpUtility.UrlEncode(_Excerpt));
			postVariables.Append("&blog_name=").Append(HttpUtility.UrlEncode(_BlogName));
			byte[] bytesToPost = new UTF8Encoding().GetBytes(postVariables.ToString());
			return bytesToPost;
		}

		public override string ToString()
		{
			return string.Format("WebUrl: {0}, ItemId: {1}, Title: {2}, Url: {3}, Excerpt: {4}, BlogName: {5}", 
				WebUrl, ItemId, Title, Url, Excerpt, BlogName);
		}
	}
}